<template>
  <div class="order-index">
    <el-form
      ref="form"
      :inline="true"
      :model="form"
      class="search-form"
    >
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="date"
          size="small"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="卡劵名称">
        <el-input
          v-model="form.key"
          size="small"
          placeholder="请输入卡劵名称，可选"
        ></el-input>

      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          @click="search(1)"
        >查询
        </el-button>
      </el-form-item>
    </el-form>
    <div class="operate-bar">
      <el-button
        size="small"
        @click="editCoupon({})"
      >创建卡劵
      </el-button>
    </div>
    <el-table
      :data="tableData"
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="卡劵ID"
        width="80"
      >
      </el-table-column>
      <el-table-column
        prop="name"
        label="卡劵名称"
      >
      </el-table-column>
      <el-table-column
        prop="price"
        label="卡劵面值"
        width="80"
      >
      </el-table-column>
      <el-table-column
        label="有限期"
        width="200px"
      >
        <template slot-scope="scope">
          <span>
            {{scope.row.time_begin}}
            <br>
            ~ {{scope.row.time_end}}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="count"
        label="数量"
        width="80"
      >
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="创建时间"
        width="170"
      >
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建人"
        width="150"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        width="210px"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="success"
            @click="editCoupon(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            type="primary"
            @click="toggleCouponState(scope.row, scope.row.state === 1 ? 2 : 1)"
          >
            {{scope.row.state === 1 ? '停用' : '启用'}}
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="toggleCouponState(scope.row, 3)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        :page-size.sync="form.size"
        :current-page.sync="form.page"
        :total="total"
        :hide-on-single-page="true"
        @current-change="search"
      >
      </el-pagination>
    </div>
    <coupon-edit/>
  </div>
</template>

<script>
  /**
   * @Author: lifuzhao
   * @Date: 2019-07-14
   * @Project: driver
   */
  import {pageSize, dateValueFormat} from "../../constant"
  import couponEdit from './parts/edit'

  export default {
    name: "index"
    , data() {
      return {
        dateValueFormat,
        form: {
          page: 1
          , size: pageSize
          , key: ''
          , time_begin: ''
          , time_end: ''
        }
        , total: 0
        , date: []
        , tableData: []
        , loading: false
      }
    }
    , watch: {
      date(date) {
        let start = ''
          , end = ''
        if (date) {
          [start, end] = date
        }
        this.form.time_begin = start
        this.form.time_end = end
      }
      , $route: {
        deep: true
        , immediate: true
        , handler() {
          this.search(1)
        }
      }
    }
    , mounted() {
      this.init()
    }
    , methods: {
      init() {
        this.$bus.$on('coupon-list-update', () => {
          this.getList()
        })
      }
      , search(page) {
        this.form.page = page
        this.getList()
      }
      , getList() {
        this.loading = true
        this.$store.dispatch('getCouponList', {
          params: this.form
          , abort: true
        })
          .then(res => {
            let {total = 0, data = []} = res
            this.total = total
            this.tableData = data
          })
          .catch(res => {

          })
          .finally(() => {
            this.loading = false
          })
      }
      , toggleCouponState(coupon, state) {
        let operation
        switch (state) {
          case 1:
            operation = '启用'
            break
          case 2:
            operation = '停用'
            break
          case 3:
            operation = '删除'
            break
        }
        this.$confirm(`是否确认${operation} ${coupon.name} ? `, '操作卡劵')
          .then(() => {
            this.$fn.setTitle(`正在${operation}${coupon.name}`)
            this.$axios.post('/api/v1/ticket/handel', {
              id: coupon.id
              , state: state
            })
              .then(res => {
                if (res.success) {
                  this.$message({
                    message: `${operation}成功`
                    , type: 'success'
                  })
                  if (state === 3) {
                    this.search(1)
                  } else {
                    this.getList()
                  }
                } else {
                  this.$message({
                    message: `${operation}失败` + res.msg
                    , type: 'error'
                  })
                }
              })
              .catch(() => {
                this.$message({
                  message: `${operation}失败` + '，请稍后再试'
                  , type: 'error'
                })
              })
              .finally(() => {
                this.$fn.setTitle('卡劵设置')
              })
          })
          .catch(() => {
          })
      }
      , editCoupon(coupon) {
        this.$bus.$emit('coupon-edit', coupon)
      }
    }
    , components: {
      couponEdit
    }
  }
</script>

<style scoped>

</style>